function debounce(el, binding) {
  const { value, arg = 500 } = binding;
  let timer;
  const handler = () => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      value && value();
    }, Number(arg));
  };
  el.handler = handler;
  el.addEventListener('click', handler);
}

export default {
  inserted(el, binding) {
    debounce(el, binding);
  },
  unbind(el) {
    el.removeEventListener('click', el.handler);
  }
};
